package linuxbox.common.taglib;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;
import java.io.*;

import linuxbox.BoxStatic;

public class Page extends BodyTagSupport
{
  protected int curPage;
  protected int totalPage;
  protected String action = null;

  public void setCurPage(int i)
  {
    curPage = i;
  }

  public void setTotalPage(int i)
  {
    totalPage = i;
  }

  public void setAction(String s)
  {
    action = s;
  }

  public int doStartTag() throws JspException
  {
    try
    {
      JspWriter out = pageContext.getOut();

      out.print("Showing page ");
      out.print(curPage+1);
      out.print(" of ");
      out.print(totalPage);
      out.print(" &nbsp;|&nbsp;");
      if (curPage >0 )
      {
        out.print("<a href=\"");
        out.print(action);
        out.print("?PAGE=0\">First</a>&nbsp;|&nbsp;");
        out.print("<a href=\"");
        out.print(action);
        out.print("?PAGE=");
        out.print(curPage-1);
        out.print("\">Prev</a>&nbsp;|&nbsp;");
      }
      else
      {
        out.print("First&nbsp;|&nbsp;Prev&nbsp;|&nbsp;");
      }
      if (curPage < totalPage -1 )
      {
        out.print("<a href=\"");
        out.print(action);
        out.print("?PAGE=");
        out.print(curPage+1);
        out.print("\">Next</a>&nbsp;|&nbsp;");
        out.print("<a href=\"");
        out.print(action);
        out.print("?PAGE=");
        out.print(totalPage-1);
        out.print("\">Last</a>");
      }
      else
      {
        out.print("Next&nbsp;|&nbsp;Last");
      }

      out.flush();
    }
    catch(Exception e)
    {
      e.printStackTrace();
      throw new JspException(e.getMessage());
    }

    return SKIP_BODY;
  }
}